package com.pearadmin.pro.modules.ser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.pearadmin.pro.common.constant.ConstantPool;
import com.pearadmin.pro.common.tools.core.StringUtil;
import com.pearadmin.pro.modules.ser.domain.Coffer;
import com.pearadmin.pro.modules.ser.domain.ServerInfo;
import com.pearadmin.pro.modules.ser.pojo.SSHConnectInfo;
import com.pearadmin.pro.modules.ser.pojo.WebSSHData;
import com.pearadmin.pro.modules.ser.service.CofferService;
import com.pearadmin.pro.modules.ser.service.ServerInfoService;
import com.pearadmin.pro.modules.ser.service.WebSSHService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Author: didiplus
 * Email: 972479352@qq.com
 * CreateTime: 2022/3/8
 * Desc:
 */
@Service
public class WebSSHServiceImpl implements WebSSHService {

    //存放ssh连接信息的map
    private static Map<String, Object> sshMap = new ConcurrentHashMap<>();

    private Logger logger = LoggerFactory.getLogger(WebSSHServiceImpl.class);
    //线程池
    private ExecutorService executorService = Executors.newCachedThreadPool();


    @Resource
    ServerInfoService serverInfoService;

    @Resource
    CofferService cofferService;


    /**
     * @Description: 初始化连接
     * @Param: [session]
     * @return: void
     */
    @Override
    public void initConnection(WebSocketSession session) {
        JSch jSch = new JSch();
        SSHConnectInfo sshConnectInfo = new SSHConnectInfo();
        sshConnectInfo.setJSch(jSch);
        sshConnectInfo.setWebSocketSession(session);
        String uuid = String.valueOf(session.getAttributes().get(ConstantPool.USER_UUID_KEY));
        //将这个ssh连接信息放入map中
        sshMap.put(uuid,sshConnectInfo);
    }

    /**
     * @Description: 处理客户端发送的数据
     * @Param: [buffer, session]
     * @return: void
     */
    @Override
    public void recvHandle(String buffer, WebSocketSession session) {
        ObjectMapper objectMapper = new ObjectMapper();
        WebSSHData webSSHData = null;
        try {
            webSSHData = objectMapper.readValue(buffer, WebSSHData.class);
            if (webSSHData.getOperate().equals("connect")){
                QueryWrapper<ServerInfo> wrapper=new QueryWrapper<>();
                List<ServerInfo> serverInfos = serverInfoService.list(wrapper.eq("ip", webSSHData.getHost()));
                Coffer coffer = cofferService.getById(serverInfos.get(0).getUserId());
                webSSHData.setUsername(coffer.getUsername());
                webSSHData.setPassword(coffer.getPassword());
                //StringUtil.isNotEmpty(webSSHData.getPort().toString()) ? webSSHData.setPort(22):webSSHData.setPort(webSSHData.getPort());
                webSSHData.setPort(22);
            }
            System.out.println("webSSHData = " + buffer );
            System.out.println(webSSHData);

        } catch (IOException e) {
            logger.error("Json转换异常");
            logger.error("异常信息:{}", e.getMessage());
            return;
        }
        String userId = String.valueOf(session.getAttributes().get(ConstantPool.USER_UUID_KEY));
        if (ConstantPool.WEBSSH_OPERATE_CONNECT.equals(webSSHData.getOperate())) {
            //找到刚才存储的ssh连接对象
            SSHConnectInfo sshConnectInfo = (SSHConnectInfo) sshMap.get(userId);
            //启动线程异步处理
            WebSSHData finalWebSSHData = webSSHData;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        connectToSSH(sshConnectInfo, finalWebSSHData, session);
                    } catch (JSchException | IOException e) {
                        logger.error("webssh连接异常");
                        logger.error("异常信息:{}", e.getMessage());
                        close(session);
                    }
                }
            });
        } else if (ConstantPool.WEBSSH_OPERATE_COMMAND.equals(webSSHData.getOperate())) {
            String command = webSSHData.getCommand();
            SSHConnectInfo sshConnectInfo = (SSHConnectInfo) sshMap.get(userId);
            if (sshConnectInfo != null) {
                try {
                    transToSSH(sshConnectInfo.getChannel(), command);
                } catch (IOException e) {
                    logger.error("webssh连接异常");
                    logger.error("异常信息:{}", e.getMessage());
                    close(session);
                }
            }
        } else {
            logger.error("不支持的操作");
            close(session);
        }
    }

    @Override
    public void sendMessage(WebSocketSession session, byte[] buffer) throws IOException {
        session.sendMessage(new TextMessage(buffer));
    }

    @Override
    public void close(WebSocketSession session) {
        String userId = String.valueOf(session.getAttributes().get(ConstantPool.USER_UUID_KEY));
        SSHConnectInfo sshConnectInfo = (SSHConnectInfo) sshMap.get(userId);
        if (sshConnectInfo !=null) {
            //断开连接
            if (sshConnectInfo.getChannel() != null) sshConnectInfo.getChannel().disconnect();
            //map中移除
            sshMap.remove(userId);
        }
    }

    /**
     * @Description: 使用jsch连接终端
     * @Param: [cloudSSH, webSSHData, webSocketSession]
     * @return: void
     */
    private void connectToSSH(SSHConnectInfo sshConnectInfo,WebSSHData webSSHData,WebSocketSession webSocketSession) throws JSchException,IOException{
        Session session = null;
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        //获取jsch的会话
        session = sshConnectInfo.getJSch().getSession(webSSHData.getUsername(), webSSHData.getHost(),webSSHData.getPort() );
        session.setConfig(config);
        //设置密码
        session.setPassword(webSSHData.getPassword());

        //连接  超时时间30s
        session.connect(30000);
        //开启shell通道
        Channel channel = session.openChannel("shell");

        //通道连接 超时时间3s
        channel.connect(3000);

        //设置channel
        sshConnectInfo.setChannel(channel);

        //转发消息
        transToSSH(channel,"\r");

        //读取终端返回的信息流
        InputStream inputStream = channel.getInputStream();
        try{
            //循环读取
            byte[] buffer = new byte[1024];
            int i = 0;
            //如果没有数据来，线程会一直阻塞在这个地方等待数据。
            while ((i=inputStream.read(buffer)) != -1) {
                sendMessage(webSocketSession, Arrays.copyOfRange(buffer,0,i));
            }

        }finally {
            //断开连接后关闭会话
            session.disconnect();
            channel.disconnect();
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

    /**
     * @Description: 将消息转发到终端
     * @Param: [channel, data]
     * @return: void
     */
    public void transToSSH(Channel channel,String command) throws IOException{
        if (channel !=null) {
            OutputStream outputStream = channel.getOutputStream();

            outputStream.write(command.getBytes());
            outputStream.flush();
        }
    }

}
